package com.xinfu.newproject.adapter.home_adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ElectricityRefillActivity;
import com.xinfu.newproject.activity.HomeActivity;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.activity.VisitorInvitationActivity;
import com.xinfu.newproject.activity.WaterRefillActivity;
import com.xinfu.newproject.bean.home.bean.GetAppIconBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;

import static com.xinfu.newproject.contants.Constant.LOGIN;

/**
 *
 */
public class HomeThreeAdapter extends BaseQuickAdapter<GetAppIconBean.ResultBean.IntelligentDeviceBean, BaseViewHolder> {

    private LinearLayout linearLayout;
    private ImageView itemImageView;
    private TextView itemTextview;

    public HomeThreeAdapter() {
        super(R.layout.item_home_adapter, null);
    }

    @Override
    protected void convert(BaseViewHolder helper, GetAppIconBean.ResultBean.IntelligentDeviceBean item) {

        itemTextview = helper.getView(R.id.item_home_adapter_tv);
        itemImageView = helper.getView(R.id.item_home_adapter_iv);
        linearLayout = helper.getView(R.id.item_home_adapter_ll);

        itemTextview.setText(item.getName());
        Glide.with(mContext).load(item.getIcon()).into(itemImageView);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (item.getName()) {
                    case "水费充值":
                        if (isLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "水");
                            ((HomeActivity) mContext).startIntentActivity(WaterRefillActivity.class, bundle);
                        } else {
                            ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case "电费充值":
                        if (isLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "电");
                            ((HomeActivity) mContext).startIntentActivity(ElectricityRefillActivity.class, bundle);
                        } else {
                            ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case "访客邀约":
                        if (isLogin()) {
                            ((HomeActivity) mContext).startIntentActivity(VisitorInvitationActivity.class, null);
                        } else {
                            ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case "智能钥匙":
                        if (isLogin()) {
                            Toast.makeText(mContext, "暂不支持该功能", Toast.LENGTH_SHORT).show();
                        } else {
                            ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                }
            }
        });

    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(mContext, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(mContext, "请先登录");
            return false;
        }

        return true;
    }

}
